<?php
/*
* File: /app/controllers/components/attachment.php
*   A file uploader and thumbnailer component for CakePHP
*/
class UploaderComponent extends Object
{
	function uploadFiles($folder, $formdata, $itemId = null) {  
	    // setup dir names absolute and relative  
	    $folder_url = IMAGES.$folder;  
	    $rel_url = "img/".$folder;
	    $viewing_url = $folder;
	    // create the folder if it does not exist  
	    if(!is_dir($folder_url)) {  
	        mkdir($folder_url);  
	    }  
	          
	    // if itemId is set create an item folder  
	    if($itemId) {  
	        // set new absolute folder  
	        $folder_url = IMAGES.$folder.'/'.$itemId;   
	        // set new relative folder  
	        $rel_url = "img/".$folder.'/'.$itemId;  
	        // create directory  
	        if(!is_dir($folder_url)) {  
	            mkdir($folder_url);  
	        }  
	    }  
	      
	    // list of permitted file types, this is only images but documents can be added  
	    $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');  
	      
	    // loop through and deal with the files  
	    foreach($formdata as $file) {  
	        // replace spaces with underscores  
	        $filename = str_replace(' ', '_', $file['name']);  
	        // assume filetype is false  
	        $typeOK = false;  
	        // check filetype is ok  
	        foreach($permitted as $type) {  
	            if($type == $file['type']) {  
	                $typeOK = true;  
	                break;  
	            }  
	        }  
	          
	        // if file type ok upload the file  
	        if($typeOK) {  
	            // switch based on error code  
	            switch($file['error']) {  
	                case 0:  
	                    // check filename already exists  
	                    if(!file_exists($folder_url.'/'.$filename)) {  
	                        // create full filename  
	                        $full_url = $folder_url.'/'.$filename;  
	                        $url = $rel_url.'/'.$filename;  
	                        $viewing_url= $viewing_url.'/'.$filename;  
	                        // upload the file  
	                        $success = move_uploaded_file($file['tmp_name'], $url);  
	                    } else {  
	                        // create unique filename and upload file  
	                        ini_set('date.timezone', 'Europe/London');  
	                        $now = date('Y-m-d-s');  
	                        $full_url = $folder_url.'/'.$now.$filename;  
	                        $url = $rel_url.'/'.$now.$filename;
	                        $viewing_url= $viewing_url.'/'.$filename;    
	                        $success = move_uploaded_file($file['tmp_name'], $url);  
	                    }  
	                    // if upload was successful  
	                    if($success) {  
	                        // save the url of the file  
	                        $result['urls'][] = $viewing_url;  
	                    } else {  
	                        $result['errors'][] = "Error uploaded $filename. Please try again.";  
	                    }  
	                    break;  
	                case 3:  
	                    // an error occured  
	                    $result['errors'][] = "Error uploading $filename. Please try again.";  
	                    break;  
	                default:  
	                    // an error occured  
	                    $result['errors'][] = "System error uploading $filename. Contact webmaster.";  
	                    break;  
	            }  
	        } elseif($file['error'] == 4) {  
	            // no file was selected for upload  
	            $result['nofiles'][] = "No file Selected";  
	        } else {  
	            // unacceptable file type  
	            $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";  
	        }  
	    }  
		return $result;  
	}  
	
}
?>